
/*
 * @Author: HanYaJun
 * @Date: 2023-01-04 15:39:32
 * @Email: hanyajun@wedobest.com.cn
 * @Description: 方块预制
 */


const trace = function (...args) {
    console.log('ItemBlock【HYJ】', ...args);
}

const traceError = function (...args) {
    console.error('ItemBlock【HYJ】', ...args);
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemBlock extends cc.Component {
    /**读取 16 进制颜色 */
    private color = [{ equal: '#54FF9F', different: '#4EEE94' },
    { equal: '#191970', different: '#000080' },
    { equal: '#BEBEBE', different: '#D3D3D3' },
    { equal: '#EE7AE9', different: '#CD69C9' },
    { equal: '#363636', different: '#4F4F4F' },
    { equal: '#DAA520', different: '#B8860B' },
    { equal: '#8B4513', different: '#A0522D' },
    { equal: '#B22222', different: '#A52A2A' },
    { equal: '#9932CC', different: '#9400D3' },
    { equal: '#0000FF', different: '#0000EE' },
    ];
    /**执行次数 */
    private executeCount: number = 1;
    /**不同颜色值 */
    private diffColor: string = null;

    /**
     * @description: 当附加到一个激活的节点上或者其节点第一次激活时候调用
     */
    protected onLoad(): void {
        // 执行次数
        let progressLevel = cc.sys.localStorage.getItem('executeCount');
        if (progressLevel) {
            this.executeCount = parseInt(progressLevel);
        } else {
            cc.sys.localStorage.setItem('executeCount', this.executeCount);
        }
    }

    /**
     * @description: 方块数据初始化
     * @param {object} data {position: 位置, index: 颜色值, diffIndex: 不同颜色值设置索引}
     * @return {*}
     */
    public initData(data: { position: cc.Vec2, index: number, diffIndex: number }): void {
        this.node.setPosition(data.position);
        cc.sys.localStorage.setItem('executeCount', this.executeCount + 1);
        this.createBlockImg(data.index, data.diffIndex);
    }

    /**
     * @description: 设置方块颜色
     * @param {number} index
     * @param {number} diffIndex
     * @return {*}
     */
    private createBlockImg(index: number, diffIndex: number): void {
        // 设置颜色值
        trace('获取颜色值索引,index:', index);
        trace('设置不同颜色值索引, diffIndex:', diffIndex);
        let colorRgb = this.color[index];
        trace('创建方块次数, this.executeCount:', this.executeCount);
        if (this.executeCount !== diffIndex) {
            this.node.color = new cc.Color().fromHEX(colorRgb.equal);
        } else {
            cc.sys.localStorage.setItem('diffColor', colorRgb.different);
            trace('设置不同颜色的16进制值,colorRgb.different:', colorRgb.different);
            this.node.color = new cc.Color().fromHEX(colorRgb.different);
        }
    }

    /**
     * @description: 点击事件
     * @param {cc} event
     */
    public clickEvent(event: cc.Event.EventTouch): void {
        let name = event.target.name;
        switch (name) {
            case 'colorBlock': {
                // 不同颜色值
                let diffColors = cc.sys.localStorage.getItem('diffColor');
                if (diffColors) {
                    this.diffColor = diffColors;
                }
                trace('获取不同颜色的16进制值,this.diffColor:', this.diffColor);
                let colorBool: boolean = this.node.color.equals(new cc.Color().fromHEX(this.diffColor));
                if (colorBool) {
                    trace('颜色值相同!');
                    this.node.dispatchEvent(new cc.Event.EventCustom('correct', true));
                } else {
                    trace('颜色值不相同!');
                    this.node.dispatchEvent(new cc.Event.EventCustom('error', true));
                }
                break;
            }
            default: {
                break;
            }
        }
    }

    /**
     * @description: 当该组件被销毁时调用
     */
    protected onDestroy(): void {
    }
}
